﻿#include"snake.h" 
//设置光标的坐标
void SetPos(int x, int y)
{
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = { x, y };
	SetConsoleCursorPosition(hOutput, pos);
}

//打印欢迎界面
void Welcome()
{
	SetPos(38, 12);
	printf("欢迎来到贪吃蛇小游戏");
	SetPos(38, 20);
	system("pause");
	system("cls");
	SetPos(25, 12);
	printf("用↑ . ↓ . ← . → 分别控制蛇的移动， F3为加速，F4为减速\n");
	SetPos(25, 13);
	printf("加速将能得到更高的分数。\n");
	SetPos(38, 20);
	system("pause");
	system("cls");
}

//打印地图
void CreatMap()
{
	//上面(0,0)->(56,0)
	SetPos(0, 0);
	for (int i = 0; i < 58; i += 2)
	{
		wprintf(L"%c", WALL);
	}
	//下面(0,0)->(56,26)
	SetPos(0, 26);
	for (int i = 0; i < 58; i += 2)
	{
		wprintf(L"%c", WALL);
	}
	//左边(0,1)->(0,25)
	for (int i = 1; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%c", WALL);
	}
	//右边(56,1)->(56,25)
	for (int i = 1; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%c", WALL);
	}
}

//初始化蛇
void InitSnake(pSnake ps)
{
	pSnakeNode cur=NULL;
	//构建蛇
	for (int i = 0; i < 5; i++)
	{
	    cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("malloc");
			return;
		}

		cur->next= NULL;
		cur->x = POS_X + i*2;
		cur->y = POS_Y;

		//头插法
		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}
	//打印蛇身
	cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	//初始化蛇的其他数据
	ps->_Add = 10;
	ps->_Dir = RIGHT;
	ps->_Score = 0;
	ps->_status = OK;
	ps->_SleepTime = 200;
	ps->_pFood = NULL;
}

//构建食物
void CreateFood(pSnake ps)
{
	int x = 0;
	int y = 0;

	again:
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);  //食物的坐标必须是2的倍数

	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (cur->x == x && cur->y == y)   //食物坐标不能在蛇身上
		{
			goto again;
		}
		cur = cur->next;
	}

	pSnakeNode food = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (food == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	else
	{
		food->x = x;
		food->y = y;
		//food->next = NULL;
		SetPos(food->x, food->y);
		wprintf(L"%c", FOOD);
		ps->_pFood = food;
	}
}

//游戏开始
void Game_Start(pSnake ps)
{
	//设置窗口大小
	system("mode con cols=100 lines=30");
	//窗口名称
	system("title 贪吃蛇");
	//获取标准输出句柄
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//关闭光标
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态'
	//欢迎界面
	Welcome();
	//打印地图
	CreatMap();
	//初始化蛇
	InitSnake(ps);
	//创造第一个食物
	CreateFood(ps);
}

//打印游戏信息
void PrintGameInfo()
{
	SetPos(64, 13);
	printf("不能穿墙，不能咬到自己\n");
	SetPos(64, 14);
	printf("用↑.↓.←.→分别控制蛇的移动.");
	SetPos(64, 15);
	printf("F1 为加速，F2 为减速\n");
	SetPos(64, 16);
	printf("ESC ：退出游戏.space：暂停游戏.");
}

//游戏暂停
void pause()
{
	while (1)
	{
		Sleep(300);
		if (KEY_PRESS(VK_SPACE))
			break;
	}
}

//是食物
void EatFood(pSnake ps, pSnakeNode pnext)
{
	pnext->next = ps->_pSnake;
	ps->_pSnake = pnext;
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	ps->_Score += ps->_Add;
	free(ps->_pFood);
	ps->_pFood = NULL;
	CreateFood(ps);
}

//不是食物
void NoFood(pSnake ps, pSnakeNode pnext)
{
	pnext->next = ps->_pSnake;
	ps->_pSnake = pnext;
	pSnakeNode cur = ps->_pSnake;

	while (cur->next->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}

	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	free(cur->next);
	cur->next = NULL;
}

void KillByWall(pSnake ps)
{
	if (ps->_pSnake->x == 0 ||ps->_pSnake->x == 56 ||ps->_pSnake->y == 0 ||ps->_pSnake->y == 26)
		ps->_status = KILL_BY_WALL;
}

void KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if (cur->x == ps->_pSnake->x && cur->y == ps->_pSnake->y)
			ps->_status = KILL_BY_SELF;
		cur = cur->next;
	}
}

//蛇的移动
void SnakeMove(pSnake ps)
{
	pSnakeNode pnext = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pnext == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}
	//处理下一步结点的坐标
	switch (ps->_Dir)
	{
	case UP:
		pnext->x = ps->_pSnake->x;
		pnext->y = ps->_pSnake->y - 1;
		break;
	case DOWN:
		pnext->x = ps->_pSnake->x;
		pnext->y = ps->_pSnake->y + 1;
		break;
	case LEFT:
		pnext->x = ps->_pSnake->x-2;
		pnext->y = ps->_pSnake->y;
		break;
	case RIGHT:
		pnext->x = ps->_pSnake->x + 2;
		pnext->y = ps->_pSnake->y;
		break;
	}
	//判断下一个结点是不是食物
	if (pnext->x == ps->_pFood->x && pnext->y == ps->_pFood->y)
	{
		EatFood(ps,pnext);
	}
	else
	{
		NoFood(ps,pnext);
	}
	KillByWall(ps, pnext);
	KillBySelf(ps, pnext);
}

//游戏运行
void Game_Run(pSnake ps)
{
	//打印提示信息
	PrintGameInfo();
	do
	{
		SetPos(64, 10);
		printf("目前得分:%05d\n", ps->_Score);
		SetPos(64, 11);
		printf("每个食物分数:%02d\n", ps->_Add);

		if (KEY_PRESS(VK_UP) && ps->_Dir != DOWN)
		{
			ps->_Dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_Dir != UP)
		{
			ps->_Dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_Dir != RIGHT)
		{
			ps->_Dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_Dir != LEFT)
		{
			ps->_Dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			//游戏暂停
			pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->_status = END_NOMAL;
		}
		else if (KEY_PRESS(VK_F1))
		{
			if (ps->_SleepTime > 120)
			{
				ps->_SleepTime -= 20;
				ps->_Add += 2;
			}
		}
		else if (KEY_PRESS(VK_F2))
		{
			if (ps->_SleepTime <280)
			{
				ps->_SleepTime += 20;
				ps->_Add -= 2;
			}
		}
		Sleep(ps->_SleepTime);
		SnakeMove(ps);
	} while (ps->_status == OK);
}

//游戏结束
void Game_End(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake;
	SetPos(20, 15);
	switch (ps->_status)
	{
	case KILL_BY_WALL:
		printf("撞墙了,游戏结束!");
		break;
	case KILL_BY_SELF:
		printf("撞身子了,游戏结束!");
		break;
	case END_NOMAL:
		printf("您结束了游戏!");
		break;
	}
	//释放蛇身
	while (cur)
	{
		pSnakeNode ret = cur;
		cur = cur->next;
		free(ret);
	}
}